var koa=require('koa')
var Router=require('koa-router')
var app=new koa()
var router=new Router()
var views=require('koa-views')
var path=require('path')
var staticFile=require('koa-static')
var render=require('koa-art-template')
var DB=require('./module/db.js')
var bodyparser=require('koa-bodyparser')


// app.use(views('./views',{map:{html:'ejs'}}));

// app.use(views('views',{
//     extension:'ejs'
// }))

app.use(staticFile('static'))
//静态资源根文件夹
app.use(bodyparser())
// 添加获取表单传值的中间件


render(app,{
    root:path.join(__dirname,'views'),
    extname:'.html',
    debug:process.env.NODE_ENV !=='production'
})

app.use(async (ctx,next)=>{
    let d=new Date()
    console.log('load time: '+d.toLocaleString());
    await next();
})

/*app.use(async (ctx,next)=>{
    console.log('second');
    next();


        console.log('the function after getting new');

})*/





router.get('/',async (ctx)=>{
    ctx.body='/';
})
router.get('/user',async (ctx)=>{
    var result=await DB.find('user',{});
    await ctx.render('user',{
        list:result
    })
})



router.get('/jq',async (ctx)=>{
    ctx.cookies.set('user','masaikk',{
        path:'/'
    })

    var result=await DB.find('user',{});
    console.log(result);

    let msoTime=new Date().toLocaleString();

    await ctx.render('t',{msoTime})
})

router.get('/h',async (ctx)=>{
    let user=ctx.cookies.get('user');
    ctx.body='Hello '+user;
})

router.get('/add',async (ctx)=>{
    await ctx.render('add')
})


router.post('/doAdd',async (ctx)=>{
    // console.log(ctx.request.body);

    await DB.insert('user',ctx.request.body);
    // ctx.body='yes';
    ctx.redirect('/user')
})

router.get('/doDel',async (ctx)=>{

    let _id=ctx.query.id;
    console.log(_id);
    await DB.remove('user',{'_id':DB.getObjectID(_id)});
    ctx.redirect('/user')
})

router.post('/doEdit',async (ctx)=>{
    // console.log(ctx.request.body);
    let id=ctx.request.body._id;
    // console.log(id);
    let username=ctx.request.body.username;
    let age=ctx.request.body.age;
    let sex=ctx.request.body.sex;


    await DB.update('user',{'_id':DB.getObjectID(id)},{username,age,sex});
    // ctx.body='yes';
    ctx.redirect('/user')
})

router.get('/remove',async (ctx)=>{
    DB.remove('user',{
        'username':'lbw4',
        'age':20,
        'sex':'男',
        'status':'1',
    })
    ctx.body='rm';
})

router.get('/new',async (ctx)=>{
    var n= ctx.query['num'];
    console.log(JSON.stringify(n) * 10);
    ctx.body='NEW '+n;
})

router.get('/edit',async (ctx)=>{
    let _id=ctx.query.id;
    let data=await DB.find('user',{'_id':DB.getObjectID(_id)})
    console.log(data[0]);

    await ctx.render('edit',{
        list:data[0]
    })
})

var userRouter=require('./user/app.js')

router.use('/n',userRouter.routes()).use(userRouter.allowedMethods())

// router.get('/in',async (ctx)=>{
//     let some_data='some information should be use'
//     // ctx.status.code(404)
//     await ctx.render('index.ejs',{
//         some_data
//         // this 'await' is important and this object in includes the data
//     })
// })

router.get('/new/:id',async (ctx)=>{
    var id= ctx.params['id']
    ctx.body='NEW id='+id
})

router.post('/getData',async (ctx)=>{
    ctx.body='Hello ajax';
})




app.use(router.routes()).use(router.allowedMethods())

app.listen(3000)